package com.project.generation;

import com.project.database.DataBaseData;
import com.project.database.TableData;
import com.project.util.Config;
import com.project.util.IoUtil;
import com.project.util.Util;
import lombok.SneakyThrows;

import java.io.File;
import java.net.URI;
import java.util.List;
import java.util.regex.Matcher;

public class ResourcesGeneration {

    private Config config;
    private DataBaseData dataBaseData;
    private String resourcePath;


    public ResourcesGeneration(Config config, DataBaseData dataBaseData) {
        this.config = config;
        this.dataBaseData = dataBaseData;
    }

    public void start() {
        this.catalogGeneration();
        this.jdbcTemplateGeneration();
        this.mybatisConfigGeneration();
        this.springConfigGeneration(this.dataBaseData.getTables());
    }

    @SneakyThrows
    private void springConfigGeneration(List<TableData> tableList) {
        URI jdbcTemplatePath = this.getClass().getClassLoader()
                .getResource("template-resource/template-spring-mvc.xml").toURI();
        File file = new File(jdbcTemplatePath);
        String jdbcTemplate = IoUtil.readFile(file);

        StringBuilder pathSkip = new StringBuilder();
        for (TableData tableData : tableList) {
            pathSkip.append("    <mvc:view-controller path=\"/page/"+tableData.getTableName()
                    +"/list\" view-name=\""
                    +tableData.getTableName()
                    +"/list\"></mvc:view-controller>\n");
            pathSkip.append("    <mvc:view-controller path=\"/page/"+tableData.getTableName()
                    +"/add\" view-name=\""
                    +tableData.getTableName()
                    +"/add\"></mvc:view-controller>\n");
        }

        jdbcTemplate = jdbcTemplate.replaceAll(Util.getTemplateString("pathSkip"),pathSkip.toString());
        jdbcTemplate = jdbcTemplate.replaceAll(Util.getTemplateString("javaBasicPath"),
                config.getJavaBasicPath().replaceAll(Matcher.quoteReplacement("\\"),"."));
        jdbcTemplate = jdbcTemplate.replaceAll(Util.getTemplateString("mapperXmlPath"),
                config.getMapperXmlPath().replaceAll(Matcher.quoteReplacement("\\"),"/"));
        IoUtil.write(jdbcTemplate, this.resourcePath+"\\"+"spring-mvc.xml");

    }

    @SneakyThrows
    private void mybatisConfigGeneration() {
        URI mybatisConfigTemplatePath = this.getClass().getClassLoader()
                .getResource("template-resource/template-mybatis-config.xml").toURI();
        File file = new File(mybatisConfigTemplatePath);
        File target = new File(this.resourcePath+"\\"+"mybatis-config.xml");
        IoUtil.fileReadWrite(file, target);
    }

    @SneakyThrows
    private void jdbcTemplateGeneration(){
        URI jdbcTemplatePath = this.getClass().getClassLoader().getResource("template-resource/template-jdbc.properties").toURI();
        File file = new File(jdbcTemplatePath);
        String jdbcTemplate = IoUtil.readFile(file);
        jdbcTemplate = jdbcTemplate.replaceAll(Util.getTemplateString("driver"), config.getDriver());
        jdbcTemplate = jdbcTemplate.replaceAll(Util.getTemplateString("url"), config.getUrl());
        jdbcTemplate = jdbcTemplate.replaceAll(Util.getTemplateString("username"), config.getUsername());
        jdbcTemplate = jdbcTemplate.replaceAll(Util.getTemplateString("password"), config.getPassword());
        IoUtil.write(jdbcTemplate, this.resourcePath+"\\"+"jdbc.properties");
    }

    private void catalogGeneration() {
        this.resourcePath = config.getOutPath()+"\\"+config.getProjectName()+"\\"+config.getResourcesPath();
        File file = new File(this.resourcePath);
        file.mkdirs();
    }


}
